<?php

declare(strict_types=1);

namespace owns\interface;

use Exception;
use owns\facade\OwnsLog;
use think\queue\Job;

/**
 * 消费
 */
abstract class OwnsQueueInterface
{
    protected $job;
    /**
     * 消息队列默认调用
     * @param Job $job 当前任务对象
     * @param array $data 发布任务时自定义数据
     */
    public function fire(Job $job, array $data): void
    {
        $consumer = substr(strrchr(get_class($this), '\\'), 1);
        try {
            $this->doTask($data);
            $job->delete();
            OwnsLog::notice('[queue-success]' . $consumer, 'queue', $data);
        } catch (Exception $e) {
            if ($job->attempts() == 3) {
                $job->delete();
                OwnsLog::error('[queue-error]' . $consumer . ' - ' . $e->getMessage(), 'queue', $data);
            } else {
                $job->release(5);
            }
        }
    }
    /**
     * 业务逻辑
     * @param array $data
     */
    abstract public function doTask(array $data);
}
